package demo03;

import java.util.*;

//计算一个字符串中每个字符出现的次数
public class Demo03MapTest {
    public static void main(String[] args) {
        System.out.println("请输入一个字符串");
        String s = new Scanner(System.in).next();
        Set<Map.Entry<Character, Integer>> entrySet = findChar1(s).entrySet();
        entrySet.forEach(characterIntegerEntry -> System.out.println(characterIntegerEntry));

    }

    private static Map<Character, Integer> findChar1(String s) {
        Map<Character, Integer> map = new HashMap<>();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (map.containsKey(c) == false) {
                map.put(c, 1);
            } else {
                Integer integer = map.get(c) + 1;
                map.put(c, integer);
            }
        }
        return map;
    }

    private static Map<Character, Integer> findChar(String s) {
        boolean isSame;
        Map<Character, Integer> map = new HashMap<>();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            Set<Character> characters = map.keySet();
            Iterator<Character> iterator = characters.iterator();
            isSame = false;
            while (iterator.hasNext()) {
                if (iterator.next() == c) {
                    Integer integer = map.get(c) + 1;
                    map.put(c, integer);
                    isSame = true;
                }
            }
            if (isSame == false)
                map.put(c, 1);
        }
        return map;
    }
}
